<template>
  <div
    v-if="visible"
    class="albums-container"
    ref="elementRef"
    :style="{ top: state.top, height: state.height }"
  >
    <div class="albums-title">
      相册
      <Icon class="albums-icon" type="md-close" size="20" @click="handleClose" />
    </div>
    <ul class="albums-tab">
      <li
        v-for="item in state.tabs"
        :key="item.name"
        :class="item.name === state.active && 'active'"
        @click="handleClickTab(item.name)"
      >
        {{ item.label }}
      </li>
      <div class="block" :style="{ left: state.left }"></div>
    </ul>
    <Divider style="margin: 6px 0" />

    <div class="albums-wrapper">
      <Albums v-if="state.active === state.tabs[0].name" />
      <Layer v-if="state.active === state.tabs[1].name" />
    </div>
  </div>
</template>

<script name="albums" setup lang="ts">
import { ref, watch } from 'vue';
import Albums from '@/components/album.vue';
import Layer from '@/components/layer.vue';

const state = reactive({
  top: '10%',
  left: '5px',
  height: '80%',
  active: 'album',
  tabs: [
    {
      name: 'album',
      label: '相册',
    },
    {
      name: 'layer',
      label: '图层',
    },
  ],
});
const elementRef = ref(null);

defineProps(['visible']);
const emit = defineEmits(['handle-close']);

const handleClickTab = (name) => {
  state.active = name;
  console.log(name);

  if (name === 'album') {
    state.left = '5px';
  } else if (name === 'layer') {
    state.left = '79px';
  }
};

const handleClose = () => {
  emit('handle-close');
};

onMounted(() => {
  if (elementRef.value) {
    calculateDimensions(elementRef.value.offsetHeight);
  }
});

watch(elementRef, (el) => {
  if (el) {
    calculateDimensions(el.offsetHeight);
  }
});

const calculateDimensions = (height) => {
  const screenHeiht = window.innerHeight;
  state.top = (screenHeiht - height) / 2 + 'px';
};
</script>

<style lang="less" scoped>
.albums-container {
  position: fixed;
  top: 0;
  left: 65px;
  width: 300px;

  border: 1px solid var(--color--border-float);
  background-color: var(--color--bg-float);
  box-shadow: var(--shadow-m);
  border-radius: 8px;
  padding: 6px;
  display: flex;
  flex-direction: column;

  .albums-title {
    font-size: 12px;
    color: #999;
    margin: 6px 0;
    padding: 0 6px;
    display: flex;
    justify-content: space-between;

    .albums-icon {
      color: var(--primary-color);
      cursor: pointer;

      &:hover {
        color: var(--primary-color-hover);
      }
    }
  }

  .albums-tab {
    width: 160px;
    height: 40px;
    border-radius: 6px;
    background-color: var(--background-primary-color);
    display: flex;
    align-items: center;
    justify-content: center;
    margin-left: 70px;
    position: relative;

    li {
      width: 75px;
      height: 28px;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 12px;
      color: #999;
      cursor: pointer;
      position: relative;
      z-index: 2;

      &.active {
        color: var(--primary-color);
        font-weight: bold;
      }
    }

    .block {
      width: 75px;
      height: 28px;
      border-radius: 6px;
      background-color: var(--color--bg-float);
      box-shadow: var(--shadow-m);
      position: absolute;
      content: ' ';
      top: 5px;
      z-index: 1;
      transition: left 0.3s ease-in-out;
    }
  }

  .albums-wrapper {
    height: calc(100% - 65px);
    overflow-y: auto;
  }
}

.transition-box {
  margin-top: 32px;
  width: 100%;
  border-radius: 4px;
  background-color: #409eff;
  text-align: center;
  color: #fff;
  padding: 24px 0;
}
</style>
